package com.tc168.cp306.b06.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tc168.cp306.b06.MyApplication;
import com.tc168.cp306.b06.R;
import com.tc168.cp306.b06.Utils.SPUtils;
import com.tc168.cp306.b06.activity.AnswerActivity;
import com.tc168.cp306.b06.activity.LoginActivity;
import com.tc168.cp306.b06.bean.UserAnswerbean;
import com.tc168.cp306.b06.gen.UserAnswerbeanDao;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends Fragment {


    @BindView(R.id.m_one)
    LinearLayout mOne;
    @BindView(R.id.m_two)
    LinearLayout mTwo;
    @BindView(R.id.m_three)
    LinearLayout mThree;
    @BindView(R.id.m_four)
    LinearLayout mFour;
    @BindView(R.id.m_five)
    LinearLayout mFive;
    @BindView(R.id.m_six)
    LinearLayout mSix;
    Unbinder unbinder;
    @BindView(R.id.num)
    TextView num;

    public MainFragment() {
        // Required empty public constructor
    }

    private List<UserAnswerbean> listcuo;

    private UserAnswerbeanDao topticbeanDao;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        topticbeanDao = MyApplication.getmDaoSessiontwo().getUserAnswerbeanDao();
        listcuo = topticbeanDao.queryBuilder().where(UserAnswerbeanDao.Properties.Istrue.eq(true)).list();

        num.setText("你今天答对了" + listcuo.size() + "道题哦~请再接再厉那~");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.m_one, R.id.m_two, R.id.m_three, R.id.m_four, R.id.m_five, R.id.m_six})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.m_one:
                if (getLoginStatus()) {
                    go(0);
                } else {
                    gologin();
                }

                break;
            case R.id.m_two:
                if (getLoginStatus()) {
                    go(1);
                } else {
                    gologin();
                }


                break;
            case R.id.m_three:
                if (getLoginStatus()) {
                    go(2);
                } else {
                    gologin();
                }


                break;
            case R.id.m_four:
                if (getLoginStatus()) {
                    go(3);
                } else {
                    gologin();
                }


                break;
            case R.id.m_five:
                if (getLoginStatus()) {
                    go(4);
                } else {
                    gologin();
                }


                break;
            case R.id.m_six:
                if (getLoginStatus()) {
                    go(5);
                } else {
                    gologin();
                }

                break;
        }
    }

    private void gologin() {
        Intent a = new Intent(getActivity(), LoginActivity.class);
        startActivity(a);
    }

    //判断登录状态
    protected boolean getLoginStatus() {
        return (boolean) SPUtils.get(getActivity(), "isdeng", false);//设置等级
    }

    private void go(int i) {

        Intent a = new Intent(getActivity(), AnswerActivity.class);
        a.putExtra("type", i);
        startActivity(a);
    }
}
